<?php


namespace App\Http\Controllers\home;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Home\PaymentController;
use App\Models\Addresses;
use App\Models\Buys;
use App\Models\Carts;
use App\Models\Configs;
use App\Models\Goods;
use App\Models\IntegralLogs;
use App\Models\Mails;
use App\Models\OrderGoods;
use App\Models\Orders;
use App\Models\OrderPays;
use App\Models\ProfitLogs;

use App\Models\Users;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class OrdersController extends Controller
{
    protected $_user_info = [];//会员信息
    protected $_order_data = [];//下单数据
    protected $_post_data = [];//表单数据

    /**
     * 确认订单信息页面
     * @param url home.order.sureOrder
     * @param Request $request
     * @return bool
     */
    public function sureOrder(){
        return view('home.order.sure_order');
    }
    /**
     * 购买第一步
     * @param url home.order.buyStep1
     * @param ifcart 购买类型：0、一步购买 1、购物车购买
     * @param goods_num 商品数量
     * @param (array)order_info 如果是一步购买：下标0：商品id，下标1:购买数量  如果是购物车购买：下标0:购物车id 下标1:购买数量
     * @return (array)store_cart_list 商品信息
     * @return store_goods_total 订单总金额
     * @return store_freight_total 订单总邮费
     * @return (array)address 地址信息
     */
    public function buyStep1(Request $request){
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->where('status','<>',-1)->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');
            if ($userinfo != 1) return $this->writeJson(1,null,'HR才可以购买商品');

            $ifcart = (int)$request->get('ifcart',0);//0:一步购买 1:购物车购买
//            if($goods_type == 1 || $goods_type == 2){//普通商品购买
//                $order_info = [
//                    '0' => ['id'=>12,'num'=>10],
//                    '1' => ['0'=>17,'1'=>5],
//                    '2' => ['0'=>1,'1'=>10],
//                ];
                //json转数组
                $orders = json_decode($request->get('order_info'));//商品信息
                foreach ($orders as $k => $v){
                    $order_info[] = (array)$this->objectToArray($v);
                }
                //得到购买商品信息
                if ($ifcart == 0) {//一步购买
                    //取得POST ID和购买数量
                    $buy_items = Buys::parseItems($order_info);
                    if (empty($buy_items)) {
                        return $this->writeJson(1,null,'所购商品无效');
                    }
                    $goods_id = key($buy_items);
                    $buy_num = current($buy_items);
                    //商品信息[得到最新商品属性及促销信息]
                    $goods_info = Buys::getGoodsOnlineInfo($goods_id, intval($buy_num));
                    if (empty($goods_info)) {
                        return $this->writeJson(1,null,'商品已下架或不存在');
                    }
                    //进一步处理数组
                    $store_cart_list = [];
                    $goods_list = [];
                    $goods_list[0] = $store_cart_list[$goods_info['store_id']][0] = $goods_info;

                    $carts=[];
                    foreach ($store_cart_list as $cart){
                        $carts[] = $cart;
                    }
                    $result =  ['goods_list' => $goods_list, 'store_cart_list' => $carts];
                }elseif ($ifcart == 1) {//购物车购买
                    //取得POST ID和购买数量
                    $buy_items = Buys::parseItems($order_info);
                    if (empty($buy_items)) return $this->writeJson(1,null,'所购商品无效');
                    if (count($buy_items) > 50) return $this->writeJson(1,null,'一次最多只可购买50种商品');
                    //购物车列表
                    $params = [
                        'cart_id'=>array_keys($buy_items),
                        'user_id' => $user_id
                    ];
                    $cart_list = Carts::getCartList($params);
                    //购物车列表 [得到最新商品属性]
                    $cart_list = Buys::getCartOnlineList($cart_list);
                    if (empty($cart_list)) return $cart_list;
                    //商品列表
                    $goods_list = [];
                    $i = 0;
                    foreach ($cart_list as $key => $cart) {
                        //购买数量
                        $buy_num = $cart['num'];
                        $goods_list[$i]['goods_num'] = $buy_num;
                        $goods_list[$i]['store_id'] = $cart['store_id'];
                        $goods_list[$i]['store_name'] = $cart['store_name'];
                        $goods_list[$i]['goods_id'] = $cart['goods_id'];
                        $goods_list[$i]['goods_name'] = $cart['goods_name'];
                        $goods_list[$i]['goods_price'] = $cart['goods_price'];
                        $goods_list[$i]['goods_img'] = $cart['goods_img'];
                        $goods_list[$i]['goods_freight'] = $cart['goods_freight'];
                        $goods_list[$i]['goods_type'] = $cart['goods_type'];
                        $i++;
                    }
                    //以店铺下标归类
                    $store_cart_list = [];
                    foreach ($cart_list as $cart) {
                        $store_cart_list[$cart['store_id']][] = $cart;
                    }
                    $carts=array();
                    foreach ($store_cart_list as $cart){
                        $carts[] = $cart;
                    }
                    $result =  ['goods_list' => $goods_list, 'store_cart_list' => $carts];
                }
//            }
//            elseif ($goods_type == 3){//积分商品兑换
//                $point_id = (int)$request->get('point_id');
//                $point_num = (int)$request->get('point_num');
//
//                $point_goods = Goods::details(['id'=>$point_id]);
//                if (empty($point_goods) || $point_goods['status'] != 1) {
//                    return $this->writeJson(1,null,'该商品不存在或已下架');
//                }
//                $point_total = $point_num * $point_goods['price'];//兑换所需总积分
//                $point_lists = [];
//                $point_lists['goods_num'] = $point_num;
//                $point_lists['store_id'] = $point_goods['store_id'];
//                $point_lists['store_name'] = $point_goods['store_name'];
//                $point_lists['goods_id'] = $point_id;
//                $point_lists['goods_name'] = $point_goods['title'];
//                $point_lists['goods_price'] = $point_goods['price'];
//                $point_lists['goods_img'] = $point_goods['thumb'];
//                $point_lists['goods_freight'] = $point_goods['freight'];
//                $point_lists['goods_storage'] = $point_goods['storage'];
//                $point_lists['goods_total'] = $point_total;
//                $point_lists['goods_type'] = $point_goods['type'];
//                //进一步处理数组
//                $store_cart_list = [];
//                $goods_list = [];
//                $goods_list[0] = $store_cart_list[$point_lists['store_id']][0] = $point_lists;
//                $result =  ['goods_list' => $goods_list, 'store_cart_list' => $store_cart_list];
//            }
            //得到页面所需要数据：收货地址、商品列表等信息
            $lists = [];
            //商品金额计算(分别对每个商品、每个店铺小计)
            list($result['store_cart_list'], $store_goods_total,$store_freight_total,$store_goods_amount) = Buys::calcCartList($result['store_cart_list']);
            $order_goods_total = 0;
            $order_freight_total = 0;
            $order_goods_amount = 0;
            foreach ($store_goods_total as $k=>$v){
                $order_goods_total += $v;
            }
            foreach ($store_freight_total as $k=>$v){
                $order_freight_total += $v;
            }
            foreach ($store_goods_amount as $k=>$v){
                $order_goods_amount += $v;
            }

            $lists['store_cart_list'] = $result['store_cart_list'];//以店铺ID分组的购物车商品信息
            $lists['order_goods_amount'] = $order_goods_amount;//商品总金额
            $lists['order_freight_total'] = $order_freight_total;//订单总邮费
            $lists['order_goods_total'] = $order_goods_total;//订单总金额
            //输出用户默认收货地址
            $lists['address_info'] = Addresses::details(['user_id'=>$user_id]);
//            dd($lists);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 积分兑换
     * @param url home.order.buyPoint
     * @param goods_id 商品id
     * @param goods_num 兑换数量
     * @param address_id 地址id
     * @param order_from 来源：1PC 2wap
     */
    public function buyPoint(Request $request)
    {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->where('status','<>',-1)->first();
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');
            if ($userinfo['type'] != 1) return $this->writeJson(1,null,'HR才可以兑换商品');

            $goods_id = (int)$request->post('goods_id');
            $goods_num = (int)$request->post('goods_num');
            $address_id = (int)$request->post('address_id');
            $order_from = (int)$request->post('order_from');
            if (empty($goods_id) || empty($goods_num) || empty($address_id)) {
                return $this->writeJson(1, null, '参数错误');
            }else {
                $address_info = Addresses::details(['id'=> $address_id]);
                if ($address_info['user_id'] != $user_id) {
                    return $this->writeJson(1, null, '请选择收货地址');
                }
                //收货人信息
                $reciver_info = [];
                $reciver_info['realname'] = $address_info['realname'];
                $reciver_info['phone'] = $address_info['phone'];
                $reciver_info['area'] = $address_info['area'];
                $reciver_info['detail'] = $address_info['detail'];
            }

            $point_goods = Goods::details(['id'=>$goods_id]);
            //判断库存是否足够
            if($point_goods['storage'] < $goods_num){
                return $this->writeJson(1, null, '库存不足');
            }
            //判断积分是否足够
            $point_total = $goods_num * $point_goods['price'] + $point_goods['freight'];//兑换所需总积分
            if($userinfo['integral'] < $point_total){
                return $this->writeJson(1,null,'积分不足');
            }

            //数据入订单信息表
            $pay_sn = Buys::makePaySn($user_id);
            $order_pay  = [];
            $order_pay['pay_sn'] = $pay_sn;
            $order_pay['user_id'] = $user_id;
            $order_pay_id = OrderPays::create($order_pay);
            if (!$order_pay_id) {
                return $this->writeJson(1,null,'订单保存失败[未生成支付单]');
            }
            //数据入订单表
            $order['order_sn'] = Buys::makeOrderSn($order_pay_id['id']);
            $order['pay_sn'] = $pay_sn;
            $order['store_id'] = $point_goods['store_id'];
            $order['store_name'] = $point_goods['store_name'];
            $order['user_id'] = $user_id;
            $order['user_name'] = $userinfo['username'];
            $order['add_time'] = date('Y-m-d H:i:s');
            $order['order_number'] = $goods_num;
            $order['order_amount'] = $point_total;
            $order['pay_price'] = $point_total;
            $order['shipping_price'] = $point_goods['freight'];
            $order['order_from'] = $order_from;
            $order['address_id'] = $address_id;
            $order['reciver_info'] = serialize($reciver_info);
            $order['pay_method'] = 3;
            $order['pay_status'] = 2;
            $order['pay_time'] = date('Y-m-d H:i:s');
            $order['order_state'] = 3;
            $order_id = Orders::create($order);
            if (!$order_id) {
                return $this->writeJson(1,null,'订单保存失败[未生成订单数据]');
            }

            $order_goods['order_id'] = $order_id['id'];
            $order_goods['store_id'] = $point_goods['store_id'];
            $order_goods['user_id'] = $user_id;
            $order_goods['goods_id'] = $point_goods['id'];
            $order_goods['goods_name'] = $point_goods['title'];
            $order_goods['goods_num'] = $goods_num;
            $order_goods['goods_image'] = $point_goods['thumb'];
            $order_goods['goods_price'] = $point_goods['price'];
            $order_goods['goods_type'] = $point_goods['type'];
            $order_goods['goods_pay_price'] = $point_total;
            $insert = OrderGoods::create($order_goods);
            if (empty($insert)) {
                return $this->writeJson(1,null,'兑换操作失败');
            }

            //变更库存和销量
            Goods::where('id',$point_goods['id'])->increment('salenum',$goods_num);//销量
            Goods::where('id',$point_goods['id'])->decrement('storage',$goods_num);//库存
            Users::where('id',$user_id)->decrement('integral',$point_total);//扣除会员积分
            Goods::where('id',$point_goods['id'])->decrement('storage',$goods_num);//更新库存
            //发送站内信-积分变更通知
            $template = [
                'title' => '兑换商品扣除积分',
                'content' => '您使用"' . $point_total .'"积分兑换商品，兑换单号为：'. $order_id['order_sn'],
                'user_id' => $user_id,
                'status' => 1,
                'type' => 1,
            ];
            Mails::create($template);
            //积分日志记录
            $integral = [
                'user_id' => $user_id,
                'user_name' => $userinfo['username'],
                'integral' => '-'.$point_total,
                'desc' => '兑换商品扣除积分',
                'type' => 2,
            ];
            IntegralLogs::create($integral);
            return $this->writeJson(0,$order_id, '兑换成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 提交订单页面
     * @param url home.order.payOrder
     * @param Request $request
     * @return bool
     */
    public function payOrder(){
        return view('home.order.pay_order');
    }
    /**
     * 购买第二步
     * @param (array)cart_info 如果是一步购买：下标0：商品id，下标1:购买数量  如果是购物车购买：下标0:购物车id 下标1:购买数量
     * @param int ifcart 购买类型： 0、一步购买 1、购物车购买
     * @param int address_id 收货地址id
     * @param int pay_method 支付方式 1微信 2支付宝
     * @param int order_from 来源：1、PC 2、手机
     * @return array
     */
    public function buyStep2(Request $request)
    {
//        $post = [
//            'cart_info' => [
//                '0' => ['0'=>16,'1'=>1],
//                '1' => ['0'=>17,'1'=>5],
//                '2' => ['0'=>18,'1'=>10],
//            ],
//            'address_id' => 6,
//            'ifcart' => 1,
//            'pay_method' => 1,
//            'order_from' => 1,
//        ];
//        $this->_user_info['user_id'] = 8;
//        $this->_user_info['user_name'] = 'HR助手';

        $post = [
            'ifcart' => (int)$request->get('ifcart'),
            'address_id' => (int)$request->get('address_id'),
            'pay_method' => (int)$request->get('pay_method'),
            'order_from' => (int)$request->get('order_from'),
        ];
        //json转数组
        $orders = json_decode($request->get('order_info'));//商品信息
        foreach ($orders as $k => $v){
            $post['cart_info'][] = (array)$this->objectToArray($v);
        }
        $this->_user_info['user_id'] = auth()->guard('web')->id();
        $this->_user_info['user_name'] = auth()->guard('web')->user()->username;
        $this->_post_data = $post;
        try {
            //第1步 数据验证
            $post       = $this->_post_data;
            $user_id    = $this->_user_info['user_id'];
            $user_name  = $this->_user_info['user_name'];
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            //取得商品ID和购买数量
            $input_buy_items = Buys::parseItems($post['cart_info']);
            if (empty($input_buy_items)) return $this->writeJson(1, null, '所购商品无效');
            //验证收货地址
            $input_address_id = $post['address_id'];
            if ($input_address_id <= 0) {
                return $this->writeJson(1, null, '请选择收货地址');
            }else {
                $input_address_info = Addresses::details(['id'=> $input_address_id]);
                if ($input_address_info['user_id'] != $user_id) {
                    return $this->writeJson(1, null, '请选择收货地址');
                }
            }
            //保存数据
            $this->_order_data['pay_method']    = $post['pay_method'];//支付方式(微信、支付宝)
            $this->_order_data['address_info']  = $input_address_info;//地址信息
            $this->_order_data['order_from']    = $post['order_from'] == 2 ? 2 : 1;//来源：1PC 2wap

            //第2步 得到购买商品信息
            if ($post['ifcart'] == 1) {//购物车购买
                //购物车列表
                $params = [
                    'cart_id' => array_keys($input_buy_items),
                    'user_id' => $user_id,
                ];
                $cart_list = Carts::getCartList($params);
                //购物车列表 [得到最新商品属性及促销信息]
                $cart_list = Buys::getCartOnlineList($cart_list);
                if (empty($cart_list)) return $cart_list;
                //商品列表
                $goods_list = [];
                $i = 0;
                foreach ($cart_list as $key => $cart) {
                    $buy_num = $cart['num'];//购买数量
                    //商品信息
                    $goods_list[$i]['goods_num'] = $buy_num;
                    $goods_list[$i]['store_id'] = $cart['store_id'];
                    $goods_list[$i]['store_name'] = $cart['store_name'];
                    $goods_list[$i]['goods_id'] = $cart['goods_id'];
                    $goods_list[$i]['goods_name'] = $cart['goods_name'];
                    $goods_list[$i]['goods_price'] = $cart['goods_price'];
                    $goods_list[$i]['goods_img'] = $cart['goods_img'];
                    $goods_list[$i]['goods_freight'] = $cart['goods_freight'];
                    $i++;
                }
                //以店铺下标归类
                $store_cart_list = [];
                foreach ($cart_list as $cart) {
                    $store_cart_list[$cart['store_id']][] = $cart;
                }
            } else {//来源于直接购买
                $goods_id = key($input_buy_items);
                $buy_num = current($input_buy_items);
                //商品信息[得到最新商品属性及促销信息]
                $goods_info = Buys::getGoodsOnlineInfo($goods_id, intval($buy_num));
                if (empty($goods_info)) {
                    return $this->writeJson(1, null, '商品已下架或不存在');
                }
                //进一步处理数组
                $store_cart_list = [];
                $goods_list = [];
                $goods_list[0] = $store_cart_list[$goods_info['store_id']][0] = $goods_info;
            }
            //保存数据
            $this->_order_data['store_cart_list'] = $store_cart_list;//已店铺分组的订单详细信息
            //第3步 得到购买相关金额计算等信息
            list($store_cart_list, $store_goods_total,$store_freight_total) = Buys::calcCartList($store_cart_list);//商品金额计算(分别对每个商品、每个店铺小计)
            //判断库存
            $append_premiums_to_cart_list = Buys::appendPremiumsToCartList($store_cart_list);
            if ($append_premiums_to_cart_list === false) {
                return $this->writeJson(1,null,'抱歉，您购买的商品库存不足，请重购买');
            } else {
                list($store_cart_list, $goods_buy_quantity) = $append_premiums_to_cart_list;
            }
            //计算订单总价
            $pay_amount = 0;
            $pay_freight = 0;
            foreach ($store_goods_total as $order_amount){
                $pay_amount += floatval($order_amount);
            }
            foreach ($store_freight_total as $freight_amount){
                $pay_freight += floatval($freight_amount);
            }
            //保存数据
            $this->_order_data['order_pay_amount']     = $pay_amount;//订单总价
            $this->_order_data['order_freight_amount']     = $pay_freight;//邮费总价
            $this->_order_data['store_cart_list']       = $store_cart_list;//已店铺分组的订单详细信息

            //第4步 生成订单
            extract($this->_order_data);
            $order_list = [];//存储生成的订单数据
            //数据入订单信息表
            $pay_sn = Buys::makePaySn($user_id);
            $order_pay  = [];
            $order_pay['pay_sn'] = $pay_sn;
            $order_pay['user_id'] = $user_id;
            $order_pay_id = OrderPays::create($order_pay);
            if (!$order_pay_id) {
                return $this->writeJson(1,null,'订单保存失败[未生成支付单]');
            }
            //收货人信息
            $reciver_info = [];
            $reciver_info['realname'] = $input_address_info['realname'];
            $reciver_info['phone'] = $input_address_info['phone'];
            $reciver_info['area'] = $input_address_info['area'];
            $reciver_info['detail'] = $input_address_info['detail'];
            foreach ($store_cart_list as $store_id => $goods_list) {
                $order = [];
                $order_goods = [];
                $numm = null;
                //数据入订单表
                $order['order_sn'] = Buys::makeOrderSn($order_pay_id);
                $order['pay_sn'] = $pay_sn;
                $order['store_id'] = $store_id;
                $order['store_name'] = $goods_list[0]['store_name'];
                $order['user_id'] = $user_id;
                $order['user_name'] = $user_name;
                $order['add_time'] = date('Y-m-d H:i:s');
                $order['pay_method'] = $this->_order_data['pay_method'];
                $order['order_amount'] = $store_goods_total[$store_id];
                $order['pay_price'] = $store_goods_total[$store_id];
                $order['shipping_price'] = $store_freight_total[$store_id];
                $order['order_from'] = $this->_order_data['order_from'];
                $order['address_id'] = $input_address_info['id'];
                $order['reciver_info'] = serialize($reciver_info);
                foreach ($goods_list as $kk => $vv){
                    $numm += intval($vv['num']);
                }
                $order['order_number'] = $numm;
                $order_id = Orders::create($order);
                if (!$order_id) {
                    return $this->writeJson(1,null,'订单保存失败[未生成订单数据]');
                }
                $order['order_id'] = $order_id['id'];
                $order_list[$order_id['id']] = $order;
                //生成order_goods订单商品数据
                $i = 0;
                foreach ($goods_list as $goods_info) {
                    $order_goods[$i]['order_id'] = $order_id['id'];
                    $order_goods[$i]['store_id'] = $store_id;
                    $order_goods[$i]['user_id'] = $user_id;
                    $order_goods[$i]['goods_id'] = $goods_info['goods_id'];
                    $order_goods[$i]['goods_name'] = $goods_info['goods_name'];
                    $order_goods[$i]['goods_num'] = $goods_info['goods_num'];
                    $order_goods[$i]['goods_image'] = $goods_info['goods_img'];
                    $order_goods[$i]['goods_price'] = $goods_info['goods_price'];
                    $order_goods[$i]['goods_type'] = $goods_info['goods_type'];
                    $order_goods[$i]['created_at'] = date('Y-m-d H:i:s');
                    $order_goods[$i]['updated_at'] = date('Y-m-d H:i:s');
                    $order_goods[$i]['goods_pay_price'] = $goods_info['goods_price'] * $goods_info['goods_num'] + $goods_info['goods_freight'];
                    $i++;
                }
                $insert = OrderGoods::insert($order_goods);
                if (empty($insert)) {
                    return $this->writeJson(1,null,'订单保存失败[未生成商品数据]');
                }
                $order_list[$order_id['id']]['order_goods']=$order_goods;
            }
            //保存数据
            $this->_order_data['pay_sn'] = $pay_sn;
            $this->_order_data['order_list'] = $order_list;

            //第5步 订单后续处理
            $ifcart =   $this->_post_data['ifcart'];
            //变更库存和销量
            foreach ($goods_buy_quantity as $goods_id => $goods_num) {
                Goods::where('id',$goods_id)->increment('salenum',$goods_num);//销量
                Goods::where('id',$goods_id)->decrement('storage',$goods_num);//库存
            }
            //删除购物车中的商品
            if($ifcart == 1){
                foreach ($input_buy_items as $cart_id => $goods_num) {
                    Carts::where(['id'=>$cart_id,'user_id'=>$user_id])->delete();
                }
            }

            return $this->writeJson(0, $this->_order_data);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 订单完成页面
     * @param url home.order.completeOrder
     * @param Request $request
     * @return bool
     */
    public function completeOrder(){
        return view('home.order.complete_order');
    }

    /**
     * 订单支付
     * @param url home.order.goodsPayOrder
     * @param pay_order 订单号
     * @return bool
     */
    public function goodsPayOrder(Request $request){
        //根据订单信息，生成支付二维码
        $pay_order = $request->post('pay_order');//订单号
        $order_info = Orders::where(array('pay_sn'=>$pay_order))->first();
        $order_goods = OrderGoods::where(array('order_id'=>$order_info['id']))->first();
        $detail = $order_info['store_name'].'商品'.$order_goods['goods_name'].'支付订单';
        $notify_url = route('home.payment.goodsNotifyUrl');
        $payment = new PaymentController();
        $ret=$payment->wxPayment($pay_order, $order_info['user_id'],$order_info['pay_price'],'支付订单',$detail,$notify_url);

        if($ret['code'] == 1){
            return $this->writeJson(0,$ret['data']);
        }else{
            return $this->writeJson(1,$ret);
        }
    }


    /**
     * 对象转换数组
     * @param $e StdClass对象实例
     * @return array|void
     */
    public function objectToArray($e)
    {
        $e = (array)$e;
        foreach ($e as $k => $v) {
            if (gettype($v) == 'resource') return;
            if (gettype($v) == 'object' || gettype($v) == 'array')
                $e[$k] = (array)$this->objectToArray($v);
        }
        return $e;
    }

}